package com.lzx.iteam.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.lzx.iteam.ContactNameCardActivity;
import com.lzx.iteam.R;
import com.lzx.iteam.bean.EventRegBean;
import com.lzx.iteam.util.Constants;
import com.lzx.iteam.util.DateUtil;
import com.lzx.iteam.util.ImageLoaderInterface;
import com.lzx.iteam.util.StringUtil;
import com.lzx.iteam.widget.RoundImageView;

import java.util.ArrayList;

/**
 * Created by zhangshuai on 15/11/4.
 */
public class EventRegAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<EventRegBean> mData;

    public EventRegAdapter(Context mContext, ArrayList<EventRegBean> mData) {
        this.mContext = mContext;
        this.mData = mData;

    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        final EventRegBean eventRegBean = mData.get(i);
        if (view == null) {
            holder = new ViewHolder();
            view = LayoutInflater.from(mContext).inflate(R.layout.reg_list_item_layout, null);
            holder.mIvHead = (RoundImageView) view.findViewById(R.id.reg_item_image);
            holder.mIvHead.setRectAdius(Constants.dip2px(mContext,20));
            holder.mTvName = (TextView) view.findViewById(R.id.reg_item_name);
            holder.mTvTime = (TextView) view.findViewById(R.id.reg_item_time);
            holder.mTvTimeOut = (TextView) view.findViewById(R.id.reg_item_time_out);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        ImageLoaderInterface.imageLoader.displayImage(eventRegBean.getImage(), holder.mIvHead, ImageLoaderInterface.optionHeadImage);
        holder.mTvName.setText(eventRegBean.getName());
        holder.mTvTime.setText(DateUtil.getReplyDate(Long.parseLong(eventRegBean.getRegTime()) * 1000));
        if (eventRegBean.isTimeOut()) {
            holder.mTvTimeOut.setVisibility(View.VISIBLE);
            holder.mTvTime.setTextColor(mContext.getResources().getColor(R.color.event_reg_color_2));
        } else {
            holder.mTvTime.setTextColor(mContext.getResources().getColor(R.color.talk_item_time1));
            holder.mTvTimeOut.setVisibility(View.GONE);
        }
        holder.mIvHead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if ("0".equals(eventRegBean.getUid()) || StringUtil.isEmpty(eventRegBean.getUid())) {
                    Toast.makeText(mContext, "对不起，该用户未注册", Toast.LENGTH_LONG).show();
                } else {
                    Intent intent = new Intent(mContext, ContactNameCardActivity.class);
                    intent.putExtra("user_id", eventRegBean.getUid());
                    mContext.startActivity(intent);
                }
            }
        });

        return view;
    }

    class ViewHolder {
        private RoundImageView mIvHead;
        private TextView mTvName;
        private TextView mTvTime;
        private TextView mTvTimeOut;
    }
}
